home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap04 / SysMets2 / SysMets2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.6 KB  |  152 lines

  1. /*----------------------------------------------------
  2.    SYSMETS2.C -- System Metrics Display Program No. 2
  3.                  (c) Charles Petzold, 1998
  4.   ----------------------------------------------------*/
  5.  
  6. #define WINVER 0x0500
  7. #include <windows.h>
  8. #include "sysmets.h"
  9.  
  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13.                     PSTR szCmdLine, int iCmdShow)
  14. {
  15.      static TCHAR szAppName[] = TEXT ("SysMets2") ;
  16.      HWND         hwnd ;
  17.      MSG          msg ;
  18.      WNDCLASS     wndclass ;
  19.  
  20.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  21.      wndclass.lpfnWndProc   = WndProc ;
  22.      wndclass.cbClsExtra    = 0 ;
  23.      wndclass.cbWndExtra    = 0 ;
  24.      wndclass.hInstance     = hInstance ;
  25.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  26.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  27.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  28.      wndclass.lpszMenuName  = NULL ;
  29.      wndclass.lpszClassName = szAppName ;
  30.  
  31.      if (!RegisterClass (&wndclass))
  32.      {
  33.           MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
  34.                       szAppName, MB_ICONERROR) ;
  35.           return 0 ;
  36.      }
  37.  
  38.      hwnd = CreateWindow (szAppName, TEXT ("Get System Metrics No. 2"),
  39.                           WS_OVERLAPPEDWINDOW | WS_VSCROLL,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.  
  44.      ShowWindow (hwnd, iCmdShow) ;
  45.      UpdateWindow (hwnd) ;
  46.  
  47.      while (GetMessage (&msg, NULL, 0, 0))
  48.      {
  49.           TranslateMessage (&msg) ;
  50.           DispatchMessage (&msg) ;
  51.      }
  52.      return msg.wParam ;
  53. }
  54.  
  55. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  56. {
  57.      static int  cxChar, cxCaps, cyChar, cyClient, iVscrollPos ;
  58.      HDC         hdc ;
  59.      int         i, y ;
  60.      PAINTSTRUCT ps ;
  61.      TCHAR       szBuffer[10] ;
  62.      TEXTMETRIC  tm ;
  63.  
  64.      switch (message)
  65.      {
  66.      case WM_CREATE:
  67.           hdc = GetDC (hwnd) ;
  68.  
  69.           GetTextMetrics (hdc, &tm) ;
  70.           cxChar = tm.tmAveCharWidth ;
  71.           cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
  72.           cyChar = tm.tmHeight + tm.tmExternalLeading ;
  73.  
  74.           ReleaseDC (hwnd, hdc) ;
  75.  
  76.           SetScrollRange (hwnd, SB_VERT, 0, NUMLINES - 1, FALSE) ;
  77.           SetScrollPos   (hwnd, SB_VERT, iVscrollPos, TRUE) ;
  78.           return 0 ;
  79.  
  80.      case WM_SIZE:
  81.           cyClient = HIWORD (lParam) ;
  82.           return 0 ;
  83.  
  84.      case WM_VSCROLL:
  85.           switch (LOWORD (wParam))
  86.           {
  87.           case SB_LINEUP:
  88.                iVscrollPos -= 1 ;
  89.                break ;
  90.      
  91.           case SB_LINEDOWN:
  92.                iVscrollPos += 1 ;
  93.                break ;
  94.      
  95.           case SB_PAGEUP:
  96.                iVscrollPos -= cyClient / cyChar ;
  97.                break ;
  98.      
  99.           case SB_PAGEDOWN:
  100.                iVscrollPos += cyClient / cyChar ;
  101.                break ;
  102.      
  103.           case SB_THUMBPOSITION:
  104.                iVscrollPos = HIWORD (wParam) ;
  105.                break ;
  106.      
  107.           default :
  108.                break ;
  109.           }
  110.  
  111.           iVscrollPos = max (0, min (iVscrollPos, NUMLINES - 1)) ;
  112.  
  113.           if (iVscrollPos != GetScrollPos (hwnd, SB_VERT))
  114.           {
  115.                SetScrollPos (hwnd, SB_VERT, iVscrollPos, TRUE) ;
  116.                InvalidateRect (hwnd, NULL, TRUE) ;
  117.           }
  118.           return 0 ;
  119.  
  120.      case WM_PAINT:
  121.           hdc = BeginPaint (hwnd, &ps) ;
  122.      
  123.           for (i = 0 ; i < NUMLINES ; i++)
  124.           {
  125.                y = cyChar * (i - iVscrollPos) ;
  126.      
  127.                TextOut (hdc, 0, y,
  128.                         sysmetrics[i].szLabel,
  129.                         lstrlen (sysmetrics[i].szLabel)) ;
  130.      
  131.                TextOut (hdc, 22 * cxCaps, y,
  132.                         sysmetrics[i].szDesc,
  133.                         lstrlen (sysmetrics[i].szDesc)) ;
  134.      
  135.                SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
  136.      
  137.                TextOut (hdc, 22 * cxCaps + 40 * cxChar, y, szBuffer,
  138.                         wsprintf (szBuffer, TEXT ("%5d"),
  139.                              GetSystemMetrics (sysmetrics[i].iIndex))) ;
  140.      
  141.                SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
  142.           }
  143.           EndPaint (hwnd, &ps) ;
  144.           return 0 ;
  145.  
  146.      case WM_DESTROY:
  147.           PostQuitMessage (0) ;
  148.           return 0 ;
  149.      }
  150.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  151. }
  152.